package com.yc.ssq.servlet;

//import com.ly.common.util.DBHelper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SsqBiz {

    public void save (String filepath) throws IOException {
        // BufferedReader 缓存字符流 可以按行读取
        // 读取文件
        try(FileReader fr = new FileReader(filepath);
            BufferedReader br = new BufferedReader(fr);
            ){
            // 解析文件
            String line;
            while ((line = br.readLine()) != null) {
                String[] items = line.split("\\s+");
                String sql = "insert into ssq_lottery values(" +
                        "default,?,?,?,?,?,?,?,?,?,?,?,?)";
                // 保存到数据库
                DBHelper.update(sql, items);
            }
        }
    }

    public List<Map<String,Object>>query(String startdate, String endsdate){
        DBHelper db = new DBHelper();
        String sql = "select * from ssq_lottery where opendate BETWEEN ? and ? order by id desc limit 0, 10";
        List<Object> params = new ArrayList<>();
        if (startdate!=null && startdate.trim().isEmpty()==false){
            params.add(startdate);
        }
        if (endsdate!=null && endsdate.trim().isEmpty()==false){
            params.add(endsdate);
        }
        return db.selectListMap(sql,params.toArray());
    }

    public List<Map<String,Object>> findByPage(int page, int rows) {
        DBHelper db = new DBHelper();
        String sql = "select * from ssq_lottery  order by id desc limit ?, ?";
        // limit mysql的分页 page 当前页码数 rows 每页显示数 每页显示5页 1页-> 0,5  2页->5, 5
        return db.selectListMap(sql, (page-1)*rows,rows);
    }

    public long total() {
        DBHelper db = new DBHelper();
        String sql = "select count(id) from ssq_lottery";
        return db.total(sql);
    }

}
